package com.xuhai.blog.common.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2022/1/26/026
 */
@Configuration
public class HttpMessageConfig {
    @Bean
    public HttpMessageConverters fastJsonConverters(){
        /*初始化FastJson解析器*/
        FastJsonHttpMessageConverter fastJsonConverter=new FastJsonHttpMessageConverter();
        /*初始化配置器*/
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        /*设置序列化*/
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        /*设置日期格式*/
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        /*装载配置文件*/
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);
        /*解决中文乱码*/
        List<MediaType> fastMediaList=new ArrayList<>();
        fastMediaList.add(MediaType.APPLICATION_JSON_UTF8);
        /*装载中文格式*/
        fastJsonConverter.setSupportedMediaTypes(fastMediaList);
        /*转换类型*/
        HttpMessageConverter<?> converter=fastJsonConverter;
        return new HttpMessageConverters(converter);
    }


}
